<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- <span>admin</span> -->
        <span>{{ editForm.userName }}</span>
      </div>
      <div style="display: flex; justify-content: center">
        <img title="点击修改用户头像" :src="squareUrl" />
      </div>
      <div style="margin-top: 15px; margin-bottom: 15px">
        <table>
          <tr>
            <td>个人编号:</td>
            <td>
              <el-tag>{{ editForm.userId }}</el-tag>
            </td>
          </tr>
          <tr>
            <td>真名:</td>
            <td>
              <el-tag>{{ editForm.realName }}</el-tag>
            </td>
          </tr>
          <tr>
            <td>电话号码:</td>
            <td>
              <el-tag>{{ editForm.phone }}</el-tag>
            </td>
          </tr>
          <tr>
            <td>电子邮箱:</td>
            <td>
              <el-tag>{{ editForm.email }}</el-tag>
            </td>
          </tr>
          <tr>
            <td>用户标签:</td>
            <td>
              <el-tag>{{ editForm.levelId }}</el-tag>
            </td>
          </tr>
        </table>
      </div>
      <div
        style="display: flex; justify-content: space-around; margin-top: 10px"
      >
        <el-button type="primary" @click="showUpdateAdminInfo">
          修改信息
        </el-button>
        <el-button type="danger">修改密码</el-button>
      </div>
    </el-card>
    <!--修改信息对话框-->
    <el-dialog
      title="编辑用户信息"
      :visible.sync="dialogVisible"
      width="50%"
      :options="userInfo"
    >
      <!--内容主体区域-->
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="70px"
      >
        <el-form-item label="用户编号">
          <el-input disabled v-model="editForm.userId"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="editForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="真名">
          <el-input v-model="editForm.realName"></el-input>
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="editForm.email"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="editForm.address"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'AdminInfo',
  data() {
    // 验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      // 验证邮箱的正则表达式
      const regEmail = /^([a-z0-9_-]+)@([\da-z-]+)\.([a-z]{2,6})$/
      if (regEmail.test(value)) {
        // 合法的邮箱
        return cb()
      }
      cb(new Error('请输入合法的邮箱'))
    }
    // 验证手机号的规则
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile = /^(0|86|17951)?(13[0-9]|15[0123456789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
      if (regMobile.test(value)) {
        return cb()
      }
      cb(new Error('请输入合法的手机号'))
    }
    return {
      squareUrl:
        'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
      userInfo: {},
      editForm: {
        userId: '',
        userName: '',
        realName: '',
        phone: '',
        email: '',
        address: ''
      },
      // 控制修改用户对话框的显示与隐藏
      dialogVisible: false,
      editFormRules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        realName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' }
        ], // 验证规则：validitor；触发时期：在失去焦点的时候
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.showAdminInfo()
  },
  methods: {
    async showUpdateAdminInfo() {
      this.dialogVisible = true
      console.log('getUserAdmin被调用')
      const { data: res } = await this.$axios.get(
        '/api/login/logininfo?username=feng'
      )
      this.editForm = res
      // this.dialogVisible = true
    },
    async showAdminInfo() {
      console.log('getUserAdmin被调用')
      const { data: res } = await this.$axios.get(
        '/api/login/logininfo?username=feng'
      )
      this.editForm = res
    },
    // 监听修改用户对话框的关闭事件
    // editDialogClosed() {
    //   this.$refs.editFormRef.resetFields()
    // },
    // 修改用户信息并提交
    editUserInfo() {
      // 预校验
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return console.log(valid)
        // 可以发起修改用户信息的数据请求
        const { data: res } = await this.$axios.post(
          '/api/admin/getNewUser',
          this.editForm
        )
        console.log(res)
        // if (res.meta.status !== 200) {
        //   this.$message.error('更新用户信息失败')
        // }
        // 刷新数据列表
        // this.showUpdateAdminInfo()
        // 提示修改成功
        this.$message.success('更新用户信息成功')
      })
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.text {
  font-size: 14px;
}
.box-card {
  width: 480px;
}
img {
  width: 100px;
  height: 100px;
  border-radius: 50px;
}
.el-tag {
  margin-top: 5px;
}
</style>
